package com.jinhao.crowd.mvc.handler;

import com.jinhao.crowd.entity.Role;
import com.jinhao.crowd.service.api.AdminService;
import com.jinhao.crowd.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created on 2021/10/2.
 *
 * @author Valar Morghulis
 */

@Controller
public class AssignHandler {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/assign/do/role/assign.html")
    public String addRelationship(@RequestParam("adminId") Integer adminId,
                                  @RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("keyword") String keyword,
                                  @RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList) {

        adminService.saveRelationship(adminId, roleIdList);

        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    @RequestMapping("/assign/to/role/page.html")
    public String getRoleByAdmin(@RequestParam("adminId") Integer adminId,
                                 ModelMap modelMap) {

        List<Role> assignedRoleList = roleService.getAssignedRoles(adminId);
        List<Role> unAssignedRoleList = roleService.getUnAssignedRoles(adminId);

        modelMap.addAttribute("assignedRoleList", assignedRoleList);
        modelMap.addAttribute("unAssignedRoleList", unAssignedRoleList);

        return "assign-role";
    }
}
